package cn.maiyar.mybatis.metadata;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import cn.maiyar.common.constant.CommonConstant;
import cn.maiyar.mybatis.MybatisPlusAutoFillProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * mybatis 通用字段 附加
 *
 * @author c.c
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class CustomMetaObjectHandler implements MetaObjectHandler {

    private final MybatisPlusAutoFillProperties autoFillProperties;

    /**
     * 是否开启了插入填充
     *
     * @return
     */
    @Override
    public boolean openInsertFill() {
        return autoFillProperties.getEnableInsertFill();
    }

    /**
     * 是否开启了更新填充
     *
     * @return
     */
    @Override
    public boolean openUpdateFill() {
        return autoFillProperties.getEnableUpdateFill();
    }

    /**
     * 插入填充，字段为空自动填充
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        Object createBy = getFieldValByName(autoFillProperties.getCreateByField(), metaObject);
        Object createTime = getFieldValByName(autoFillProperties.getCreateTimeField(), metaObject);
        Object updateBy = getFieldValByName(autoFillProperties.getUpdateByField(), metaObject);
        Object updateTime = getFieldValByName(autoFillProperties.getUpdateTimeField(), metaObject);
        if (createTime == null || updateTime == null || createBy == null || updateBy == null) {
            if (createTime == null) {
                setFieldValByName(autoFillProperties.getCreateTimeField(), LocalDateTime.now(), metaObject);
            }
            if (createBy == null) {
                setFieldValByName(autoFillProperties.getCreateByField(), getUid(), metaObject);
            }
            if (updateTime == null) {
                setFieldValByName(autoFillProperties.getUpdateTimeField(), LocalDateTime.now(), metaObject);
            }
            if (updateBy == null) {
                setFieldValByName(autoFillProperties.getUpdateByField(), getUid(), metaObject);
            }
        }
    }

    /**
     * 更新填充
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        setFieldValByName(autoFillProperties.getUpdateTimeField(), LocalDateTime.now(), metaObject);
        setFieldValByName(autoFillProperties.getUpdateByField(), getUid(), metaObject);
    }

    /**
     * 获取用户
     *
     * @return
     */
    private String getUid() {
        String username = "unknown_user";
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = (requestAttributes == null) ? null : ((ServletRequestAttributes) requestAttributes).getRequest();
        if (request != null) {
            username = request.getHeader(CommonConstant.USER_NAME_HEADER);
        }
        return username;
    }
}
